package 网络编程多线程之聊天;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive implements Runnable{

	private DatagramSocket ds;
	public Receive(DatagramSocket ds){
		this.ds = ds;
	}
	public void run() {
		try {
			while(true){
				byte[] buf = new byte[1024];
				DatagramPacket dp = new DatagramPacket(buf,buf.length);
				ds.receive(dp);
				String ip = dp.getAddress().getHostAddress();
				String data = new String(dp.getData(),0,dp.getLength());
				System.out.println("IP是："+ip+"的哥们说："+data);
			}
		} catch (Exception e) {
			throw new RuntimeException("接收失败");
		}

	}
}
